layui.use(['table', 'admin', 'ax', 'func','form','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var laydate = layui.laydate;


    /**
     * 管理
     */
    var BtyZxFeiyongfentan = {
        tableId: "btyZxFeiyongfentanTable"
    };

    //年月选择器
    laydate.render({
        elem: '#timeLimit',
        type: 'month',
        done: function(value, date, endDate) {
            var val = value;
            $('#timeLimit1').val(val);
            BtyZxFeiyongfentan.search();

        }
    });

    /**
     * 初始门店
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyZxFeiyongfentan/dmList");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    var kh = '';
    var kyrq = '';
    $.each(languageResult.data, function (index, val) {
        //查询需要选择的门店
        $("#name").append("<option value='" + val.yaoma + "'>" + val.dm + "</option>");
    });

    form.render('select');


    /**
     * 初始化表格的列
     */
    BtyZxFeiyongfentan.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers',align:'center', title: '序号'},
            {field: 'id', hide: true, title: ''},
            {field: 'yaoma', title: '妖码'},
            {field: 'name', title: '门店名称'},
            {field: 'kyrq', title: '开业日期'},
            {field: 'type', title: '类型', templet: function (d) {
                    if (d.type == '0') {
                        return "店面房租";
                    } else if (d.type == '1') {
                        return "工资";
                    } else if (d.type == '2') {
                        return "配送进货";
                    }
                }},
            {field: 'cost', title: '装修费用'},
            // {field: 'kemuId', title: '科目id'},
            // {field: 'keywordId', title: '关键字id'},
            {field: 'kh', title: '卡号'},
            {field: 'status', title: '状态', hide: true},
            // {field: 'status', title: '状态', templet: function (d) {
            //         if (d.status == '1') {
            //             return "已生成";
            //         } else if (d.status == '2') {
            //             return "未生成";
            //         }
            //     }},
            // {field: 'createTime', title: ''},
            // {field: 'createUser', title: ''},
            // {field: 'updateTime', title: ''},
            // {field: 'updateUser', title: ''},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyZxFeiyongfentan.search = function () {
        var queryData = {};

        queryData['month'] = $('#timeLimit1').val();
        queryData['yaoma'] = $('#name').val();
        queryData['type'] = $('#type').val();
        queryData['cost'] = $('#cost').val();

        table.reload(BtyZxFeiyongfentan.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */

    form.on('submit(btnSubmit)', function (data) {
        var ajax1 = new $ax(Feng.ctxPath + "/btyZxFeiyongfentan/addItem", function (data) {
            if (data.code == 1) {
                Feng.error("添加失败!" + data.message + "!");
                return;
            }
            Feng.success("添加成功!");
            $("#cost").val('');
            $("#name").val('');
            form.render();
            table.render({
                elem: '#' + BtyZxFeiyongfentan.tableId,
                url: Feng.ctxPath + '/btyZxFeiyongfentan/list',
                page: true,
                height: "full-158",
                cellMinWidth: 100,
                limit:20,
                cols: BtyZxFeiyongfentan.initColumn(),
                where: {
                    type: $('#type').val()
                }
            });

        }, function (data) {
            Feng.error("添加失败!" + data.responseJSON.message + "!");
        });
        ajax1.set("timeLimit", $('#timeLimit1').val());
        ajax1.set("name", $("#name").find("option:selected").text());
        ajax1.set("yaoma", $("#name").find("option:selected").val());
        ajax1.set("type", $("#type").find("option:selected").val());
        ajax1.set("cost", $("#cost").val());
        ajax1.set("kh", kh);
        ajax1.set("kyrq", kyrq);
        ajax1.start();
    })


    // form.on('select(language)', function (data) {
    //     BtyZxFeiyongfentan.search();
    //
    // });


    form.on('select(name)', function(data){
        // BtyZxFeiyongfentan.search();
        var languageAjax = new $ax(Feng.ctxPath + "/btyZxFeiyongfentan/dmList");
        var languageResult = languageAjax.start();
        console.info(languageResult);

        for (let i= 0;i< languageResult.data.length;i++){
            // $("#name").append("<option value='" + languageResult.data[i].yaoma + "'>" + languageResult.data[i].dm + "</option>");

            if ($("#name").find("option:selected").val() == languageResult.data[i].yaoma){
                kh = languageResult.data[i].kh;
                kyrq = languageResult.data[i].kyrq;
            }
        }
        form.render('select');
    });

    /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyZxFeiyongfentan.openEditDlg = function (data) {
          func.open({
              title: '修改',
              content: Feng.ctxPath + '/btyZxFeiyongfentan/edit?id=' + data.id,
              tableId: BtyZxFeiyongfentan.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    BtyZxFeiyongfentan.exportExcel = function () {
        var checkRows = table.checkStatus(BtyZxFeiyongfentan.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyZxFeiyongfentan.onDeleteItem = function (data) {
        var operation = function () {
           var sta = data.status;
           if (sta == 2){
               var ajax = new $ax(Feng.ctxPath + "/btyZxFeiyongfentan/delete", function (data) {

                   Feng.success("删除成功!");
                   table.reload(BtyZxFeiyongfentan.tableId);
               }, function (data) {
                   Feng.error("删除失败!" + data.responseJSON.message + "!");
               });
               ajax.set("id", data.id);
               ajax.start();
           }else if (sta == 1){
               Feng.error("删除失败!已生成报销单，不可删除！");
           }

        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyZxFeiyongfentan.tableId,
        url: Feng.ctxPath + '/btyZxFeiyongfentan/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        limit:20,
        cols: BtyZxFeiyongfentan.initColumn(),
        where: {
            month: $('#timeLimit1').val(),
            yaoma: $("#name").val(),
            type: $('#type').val(),
            cost: $('#cost').val(),
        }

    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyZxFeiyongfentan.search();
    });

    // 添加按钮点击事件
    // $('#btnAdd').click(function () {
    //
    // BtyZxFeiyongfentan.openAddDlg();
    //
    // });

    // 导出excel
    $('#btnExp').click(function () {
        BtyZxFeiyongfentan.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyZxFeiyongfentan.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyZxFeiyongfentan.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyZxFeiyongfentan.onDeleteItem(data);
        }
    });
});
